using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Resources;
using System.IO;
using System.Management;
using System.Xml;
using System.Data;
using System.Net;
using System.Security;

namespace Util.Common
{  
    public class CommonUtility
    {
        static CommonUtility()
        {
       
                IsValid = true;
        }
        internal  static bool IsValid = false;

       
        public static DateTime GetSystemDateTime(){
            return DateTime.Now;
        }
        

        public static string DateToString(string aDate) 
        {
            if (aDate == null || aDate.Length == 0 )
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_MSG);
            }

            try
            {
                try
                {
                    if (aDate.Substring(0, 2).Equals("00"))
                    {
                        return aDate.Substring(6, 4) + "0000";
                    }

                    DateTime l_datetime = DateTime.Parse(aDate);
                    return l_datetime.Year.ToString("0000") + l_datetime.Month.ToString("00") + l_datetime.Day.ToString("00");
                }
                catch (FormatException ex)
                {
                    throw ex;
                }catch(ArgumentNullException ex){
                    throw ex;
                }catch(ArgumentException ex){
                    throw ex;
                }
                catch (Exception)
                {
                    return aDate.Substring(6, 4) + aDate.Substring(3, 2) + aDate.Substring(0, 2);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        

        public static string DateToString(DateTime aDT)
        {
            try
            {                
                try
                {
                    DateTime l_datetime = aDT; 
                    return l_datetime.Year.ToString("0000") + l_datetime.Month.ToString("00") + l_datetime.Day.ToString("00");
                }
                catch (Exception)
                {
                    return ""; 
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string TimeToString(DateTime aTime)
        {
            try
            {               
                try
                {
                    DateTime l_Time = aTime;
                    return l_Time.Hour.ToString("00") + l_Time.Minute.ToString("00") + l_Time.Second.ToString("00");
                }
                catch (Exception)
                {
                    return "";
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static string StringToDate(string aString)
        {
            if (aString == null | aString.Length == 0)
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_MSG);
            }
            if (aString.Length < 8)
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_LENGTH);
            }
            try
            {
                if (aString.Substring(4, 4).Equals("0000"))
                {
                    return aString.Substring(6, 2) + "/" + aString.Substring(4, 2) + "/" + aString.Substring(0, 4);
                }
                else
                {
                    return new DateTime(int.Parse(aString.Substring(0, 4)), int.Parse(aString.Substring(4, 2)), int.Parse(aString.Substring(6, 2))).ToShortDateString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DateTime StringToDateInDateTime(string aString)
        {
            if (aString == null | aString.Length == 0)
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_MSG);
            }
            if (aString.Length < 8)
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_LENGTH);
            }
            try
            {
                
                return new DateTime(int.Parse(aString.Substring(0, 4)), int.Parse(aString.Substring(4, 2)), int.Parse(aString.Substring(6, 2)));
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DateTime StringToDateInTime(string aString)
        {
            if (aString == null | aString.Length == 0)
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_MSG);
            }
            if (aString.Length < 8)
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_LENGTH);
            }
            try
            {
                return new DateTime(int.Parse(aString.Substring(0, 4)), int.Parse(aString.Substring(4, 2)), int.Parse(aString.Substring(6, 2)), int.Parse(aString.Substring(8, 2)), int.Parse(aString.Substring(10, 2)), int.Parse(aString.Substring(12,2)));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string StringToDate(string aString,string aFormat)
        {
            if (aString == null | aString.Length == 0)
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_MSG);
            }
            if (aString.Length < 8)
            {
                throw new DateConvertException(CoreConstant.INVALID_INPUT_LENGTH);
            }
            try
            {
                if (aString.Substring(4, 4).Equals("0000"))
                {
                    return aString.Substring(6, 2) + "/" + aString.Substring(4, 2) + "/" + aString.Substring(0, 4);
                }
                else
                {
                    return new DateTime(int.Parse(aString.Substring(0, 4)), int.Parse(aString.Substring(4, 2)), int.Parse(aString.Substring(6, 2))).ToString(aFormat);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static string GetDateTimeString()
        {
            return DateToString() + TimeToString();
        }
        public static string DateToString()
        {            
            return DateToString(GetSystemDateTime());
        }
        public static string TimeToString()
        {
            return TimeToString(GetSystemDateTime());
        }
        public static string StringToDate()
        {
            return GetSystemDateTime().ToShortDateString();
        }       

        public static string GetWebURL(string aWebSite, string aPage)
        {
            if (aWebSite.EndsWith("/"))
            {
                aWebSite = aWebSite.Substring(0, aWebSite.Length - 1);
            }
            if (aPage.StartsWith("/"))
            {
                aPage = aPage.Substring(1);
            }
            return aWebSite + "/" + aPage;
        }

        public static string Encrypt(string aValue)
        {
            try
            {
                Encryptor en = new Encryptor(EncryptionAlgorithm.Rc2, CoreConstant.PRIVATE_KEY);
                return en.Encrypt(aValue) + Convert.ToChar(254) + Convert.ToBase64String(en.IV);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string Decrypt(string aValue)
        {
            try
            {
                if (aValue.Length == 0) return "";

                string[] aStr = aValue.Split(new char[] { Convert.ToChar(254) });
                byte[] IV = Convert.FromBase64String(aStr[1]);
                Decryptor dec = new Decryptor(EncryptionAlgorithm.Rc2, IV);
                return dec.Decrypt(aStr[0], CoreConstant.PRIVATE_KEY);
            }
            catch (Exception)
            {
                throw;
            }
        }


        public static bool SendEmail(string aTo, string aSubject, string aBody)
        {            
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.From = new System.Net.Mail.MailAddress("admin@club.com");
            message.To.Add(aTo);
            message.Subject = aSubject;
            message.Body = "Dear Member,<br/>Your password has been reset by Administrator upon request.<br/>Below is your new password.<br/>Password : " + aBody;
            message.IsBodyHtml = true;
            System.Net.Mail.SmtpClient l_mailClient = new System.Net.Mail.SmtpClient();
            l_mailClient.Host = "smtp.gmail.com";
            l_mailClient.Port = 587;
            NetworkCredential l_credential = new NetworkCredential("","","");
            

            l_mailClient.Credentials = l_credential;
            //l_mailClient.Send(message);
            
            return true;
        }

        public static string GetString(object value)
        {
            if (value is DBNull)
                return string.Empty;
            else if (value == null)
                return string.Empty;
            else
                return value.ToString();
        }
    }
}
